package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.config.param.Converse;
import com.sinog.biz.mapper.havexml.AreaCodeFullMapper;
import com.sinog.biz.mapper.havexml.VolunteerManageMapper;
import com.sinog.biz.service.business.VolunteerManageService;
import com.sinog.biz.util.IdGen;
import com.sinog.biz.util.smlib.Sm4;
import com.sinog.core.domain.VolunteerManageInfo;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PhoneUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 安帮工作资源管理 - 安帮工作资源管理 - 志愿者信息管理 - 志愿者信息管理 - VolunteerManageServiceImpl
 * @author -
 * @date -
 */
@Service
public class VolunteerManageServiceImpl extends ServiceImpl<VolunteerManageMapper,VolunteerManageInfo> implements VolunteerManageService {

    @Resource
    private VolunteerManageMapper volunteerManageMapper;
    @Resource
    private AreaCodeFullMapper areaCodeFullMapper;

    /**
     * 安帮工作资源管理 - 安帮工作资源管理 - 志愿者信息管理 - 志愿者信息管理 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @param departId departId
     * @return String
     */
    @Override
    public String getVolunteerInfoList(Integer pageIndex,Integer pageSize,
                                       @Converse(caseIf = "-1", converseTo = "0") String querykey,String departId) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        List<Map<String,String>> volunteerInfoList = volunteerManageMapper.getVolunteerInfoList(page,querykey,departId);
        for(Map<String,String> map : volunteerInfoList) {
            if(map.get("lxfs") != null) {
                Map<String,String> telAndPhone = PhoneUtil.splitTelAndPhone(map.get("lxfs"));
                map.put("lxfs",telAndPhone.get("phone"));
                map.put("tel",telAndPhone.get("tel"));
            }
        }
        return JsonUtil.toText(page.getTotal(),volunteerInfoList);
    }

    /**
     * 安帮工作资源管理 - 安帮工作资源管理 - 志愿者信息管理 - 志愿者信息管理 - （修改/新增）
     * @param data data
     * @return Result
     */
    @Override
    public Result saveVolunteerInfo(String data) {
        //参数实体
        VolunteerManageInfo volunteerManageInfo = JsonUtil.toBean(data,VolunteerManageInfo.class);
        volunteerManageInfo.setSfzh(Sm4.encryptEcb(volunteerManageInfo.getSfzh()));
        //对只有电话/只有固话的进行|的去除
        String lxfs = volunteerManageInfo.getLxfs();
        if (StringUtils.isNotBlank(lxfs)) {
            Character one = lxfs.charAt(0);
            Character last = lxfs.charAt(lxfs.length()-1);
            if ("|".equals(one.toString()) || "|".equals(last.toString())) {
                String lxfs1 = lxfs.replace("|","");
                volunteerManageInfo.setLxfs(lxfs1);
            }
        }
        Date date = new Date();
        //新增
        if(StringUtils.isBlank(volunteerManageInfo.getId())) {
            volunteerManageInfo.setId(IdGen.strid());
            volunteerManageInfo.setCreateTime(date);
        }
        volunteerManageInfo.setUpdateUser(volunteerManageInfo.getCreateUser());
        volunteerManageInfo.setUpdateTime(date);
        volunteerManageInfo.setBffwCode(Constants.NumberStr.NUM_STR_FU_ONE.equals(volunteerManageInfo.getBffwCode()) ? null : volunteerManageInfo.getBffwCode());
        // 保存住址省、市、县、街道名称
        Map<String,String> map = new HashMap<>(16);
        String provinceCode = volunteerManageInfo.getProvince();
        String cityCode = volunteerManageInfo.getCity();
        String countyCode = volunteerManageInfo.getCounty();
        String clabCode = volunteerManageInfo.getClab();
        if (StringUtils.isNotBlank(provinceCode)){
            Map<String,String> areaNames = areaCodeFullMapper.getAreaNames(provinceCode,cityCode,countyCode,clabCode);
            if (areaNames == null){
                return Result.failure(ReturnCode.SAVE_FAILED);
            }
            volunteerManageInfo.setProvinceName(areaNames.get("shengName"));
            volunteerManageInfo.setCityName(areaNames.get("dishiName"));
            volunteerManageInfo.setCountyName(areaNames.get("quxianName"));
            volunteerManageInfo.setClabName(areaNames.get("jiedaoName"));
            if (StringUtils.isBlank(cityCode)){
                volunteerManageInfo.setCityName("");
            }
            if (StringUtils.isBlank(countyCode)){
                volunteerManageInfo.setCountyName("");
            }
            if (StringUtils.isBlank(clabCode)){
                volunteerManageInfo.setClabName("");
            }
        } else {
            volunteerManageInfo.setProvinceName("");
            volunteerManageInfo.setCityName("");
            volunteerManageInfo.setCountyName("");
            volunteerManageInfo.setClabName("");
        }
        boolean tag = volunteerManageInfo.insertOrUpdate();
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮工作资源管理 - 志愿者信息管理 - 志愿者信息管理 - 删除
     * @param id id
     * @return Result
     */
    @Override
    public Result deleteVolunteerInfo(String id) {
        VolunteerManageInfo volunteerManageInfo = new VolunteerManageInfo();
        boolean result = volunteerManageInfo.deleteById(id);
        if(result) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮工作资源管理 - 志愿者信息管理 - 志愿者信息管理 - 导出
     * @param departId departId
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> selectVolunteerInfoForExport(String departId) {
        return volunteerManageMapper.selectVolunteerInfoForExport(departId);
    }

    /**
     * 安帮工作资源管理 - 安帮工作资源管理 - 志愿者信息管理 - 志愿者信息管理 - 查看
     * @param id id
     * @return Result
     */
    @Override
    public Result selectVolunteerInfo(String id) {
        return Result.success(volunteerManageMapper.selectById(id));
    }
}